package com.spd.id2.demo;


import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.spd.id2.impl.ConfigEntity;
import com.spd.id2.impl.IIdReadCallBack;
import com.spd.id2.impl.IdManager;
import com.spd.id2.impl.PowerEntity;
import com.spd.lib.mvp.BasePresenter;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author :Reginer  2022/8/10 0010 15:53.
 * 联系方式:QQ:282921012
 * 功能描述:
 */
public class Id2Presenter extends BasePresenter<ReadId2Activity, Id2Model> implements Id2Contract.Presenter {
    @Override
    public void loadModuleConfig() {
        getView().onLoadProgress();
        String module = getRealModule().toUpperCase();
        String urlStart = "http://webmdm.speedata.cn:9003/web-mdm/spdConfig/";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(urlStart + module + ".config").build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                try {
                    getView().runOnUiThread(() -> getView().onLoadConfigError());
                } catch (Exception exception) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) {
                try {
                    if (response.isSuccessful()) {
                        String config = Objects.requireNonNull(response.body()).string();
                        Log.i("spdId2", "config is:::" + config);
                        ConfigEntity configEntity = new Gson().fromJson(config, ConfigEntity.class);
                        getView().runOnUiThread(() -> getView().onLoadConfigSuccess(configEntity.getId2()));

                    } else {
                        getView().runOnUiThread(() -> getView().onLoadConfigError());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    public String getRealModule() {
        String showModule = Build.MODEL;
        try {
            Class<?> clazz = Class.forName("android.os.SystemProperties");
            Method method = clazz.getMethod("get", String.class);
            method.setAccessible(true);
            String realModule = (String) method.invoke(null, "ro.build.developer");
            Log.i("spdId2", "realModule is:::" + realModule);
            return TextUtils.isEmpty(realModule) ? showModule : realModule;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return showModule;

    }

    @Override
    public void initDev(@NonNull PowerEntity id2Entity, IIdReadCallBack idReadCallBack) {
        IdManager.getInstance().initDev(id2Entity, idReadCallBack);
    }

    @Override
    public void releaseDev() {
        IdManager.getInstance().releaseDev();
    }



    @Override
    public void clearUi() {
        if (getView() != null) {
            getView().successCount = 0;
            getView().idInfo.setText("");
            getView().idPhoto.setImageBitmap(null);
            getView().readId2Log.setText("");
        }
    }

    @Override
    protected Id2Model createModel() {
        return new Id2Model();
    }
}
